package agents;

import extra.Position;
import game.constants.ReferenceAgent;
import game.constants.ReferenceSpecialFeature;
import interfaces.impl.Agent;
import objects.SpetialFeature;
import objects.WorldObject;
import universe.Universe;
import actions.ActionXYtoMove;
import constants.Constants;

public class AgentMoveMonster extends Agent{
	
	//actions
	ActionXYtoMove actionXYToMove;
	//features
	private SpetialFeature xToMove;
	private SpetialFeature yToMove;
	private SpetialFeature isMoving;
	private SpetialFeature xBeginAreaToMove;
	private SpetialFeature yBeginAreaToMove;
	private SpetialFeature xEndAreaToMove;
	private SpetialFeature yEndAreaToMove;
	private SpetialFeature visionRange;
	private SpetialFeature attackObjectId;
	private SpetialFeature objectFollowId;
	
	private double contTime = 0;
	

	
	@Override
	public void initializeActionList() {
	
		actionXYToMove = new ActionXYtoMove(); 
		listActions.add(actionXYToMove);
		
	}

	@Override
	public void initializeSpecialFeatures() {
		xToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_TO_MOVE);
		listFeatures.add(xToMove);
		
		yToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_TO_MOVE);
		listFeatures.add(yToMove);
		
		isMoving = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_IS_MOVING);
		listFeatures.add(isMoving);
		
		xBeginAreaToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_BEGIN_AREA_TO_MOVE);
		listFeatures.add(xBeginAreaToMove);
		
		yBeginAreaToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_BEGIN_AREA_TO_MOVE);
		listFeatures.add(yBeginAreaToMove);
		
		xEndAreaToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_X_END_AREA_TO_MOVE);
		listFeatures.add(xEndAreaToMove);
		
		yEndAreaToMove = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_Y_END_AREA_TO_MOVE);
		listFeatures.add(yEndAreaToMove);
		
		visionRange= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_VISION_RANGE);
		listFeatures.add(visionRange);
		
		attackObjectId = new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_ATTACK);
		listFeatures.add(attackObjectId);
		
		objectFollowId= new SpetialFeature(ReferenceSpecialFeature.SPETIAL_FEATURE_TARGET_ID_TO_FOLLOW);
		listFeatures.add(objectFollowId);
 		
	}

	@Override
	public void initializeName() {
		name="AgentMoveMonster";
		
	}

	@Override
	public void initializeId() {
		id=ReferenceAgent.AGENT_MOVE_MONSTER;
		
	}

	@Override
	public void updateChild(WorldObject object, Universe universe, double time) {
		
		if( isMoving.getValue()==Constants.TRUE){
			
			return;
		}
		if(visionRange.getValue()<10){
			return;
		}
		//System.out.println("Cuento tiempo "+time);
		contTime=contTime+time;
		if(contTime>100){
			calculateNextPositionToMove(object,universe);
			contTime=0;
		}
		
	}

	private void calculateNextPositionToMove(WorldObject object,Universe universe) {
		
		if(objectFollowId.getValue()!=Constants.NO_OBJECT_ID || attackObjectId.getValue()!=Constants.NO_OBJECT_ID){
			return;
		}
		
		int x = (int) (xBeginAreaToMove.getValue()+( ((Math.random()*1000)%(xEndAreaToMove.getValue())))); 
		int y = (int) (yBeginAreaToMove.getValue()+( ((Math.random()*1000)%(yEndAreaToMove.getValue()))));
		//int x = (int) (object.getPosition().getX()+(object.getPosition().getX()- ( 2*((Math.random()*1000)%(visionRange.getValue())))));
		//int y = (int) (object.getPosition().getY()+(object.getPosition().getY()- ( 2*((Math.random()*1000)%(visionRange.getValue())))));
		Position pos = new Position(x, y);
		int cont=0;
		/*while(Position.DistanceBetween(pos, object.getPosition())>visionRange.getValue() && cont<2){
			x = (int) (object.getPosition().getX()+(object.getPosition().getX()- ( 2*((Math.random()*1000)%(visionRange.getValue())))));
			y = (int) (object.getPosition().getY()+(object.getPosition().getY()- ( 2*((Math.random()*1000)%(visionRange.getValue())))));
			pos = new Position(x, y);
			cont++;
			
		}
		if(cont>=2){
			return;
		}*/
		if(Position.DistanceBetween(pos, object.getPosition())<visionRange.getValue());
		actionXYToMove.setSource(object);
		actionXYToMove.setTarget(object);
		actionXYToMove.setxToMove(x);
		actionXYToMove.setyToMove(y);
		universe.setAction(actionXYToMove);
		
	}


	
}
